/**
 * 
 */
package com.jack.hhitseat.task;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.jack.hhitseat.bean.Seat;
import com.jack.hhitseat.bean.SeatInfo;
import com.jack.hhitseat.bean.Student;
import com.jack.hhitseat.service.impl.SeatService;
import com.jack.hhitseat.utils.MyUtils;

/**
 * @author Dell
 *
 */
@Component
@Controller
//@EnableScheduling   // 开启定时任务
@EnableAsync        // 开启多线程
public class InfoTask {
	
	@Autowired
	private SeatService seatService;
	
	//定时登录
//	@Scheduled(cron = "00 58 5 * * ?")
//	@Scheduled(cron = "00 30 9 * * ?")
//	@Scheduled(cron = "00 15 12 * * ?")
//	@Scheduled(cron = "00 50 15 * * ?")
//	@Scheduled(cron = "00 20 19 * * ?")
    public void getInfo() {
		String d = MyUtils.getNowDateYMD();
		String sjc = System.currentTimeMillis()+"";
		
		String floors[] = {"100455346","100455348","100455578",
				"100455353","100455582","100455588","100455590",
				"100455592","100455594","100455596","100455598",};
		
		for(String sname : floors) {
			String getUrl = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id=" + sname
					+"&purpose=&selectOpenAty=&cld_name=default&date=" + d
					+"&fr_start=06%3A00&fr_end=22%3A00&act=get_rsv_sta&_=" + sjc;
			save(getUrl);
		}
		
		d = MyUtils.getNextDate();
		sjc = System.currentTimeMillis()+"";
		for(String sname : floors) {
			String getUrl = "http://seat.hhit.edu.cn/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id=" + sname
					+"&purpose=&selectOpenAty=&cld_name=default&date=" + d
					+"&fr_start=06%3A00&fr_end=22%3A00&act=get_rsv_sta&_=" + sjc;
			save(getUrl);
		}
    }
	
	private void save(String getUrl) {
		StringBuffer sb = new StringBuffer();
		try {
		     InputStreamReader isr = null;
		     BufferedReader br = null;
		     URL url = new URL(getUrl);
		     URLConnection urlConnection = url.openConnection();
		     urlConnection.setAllowUserInteraction(false);
		     isr = new InputStreamReader(url.openStream());
		     br = new BufferedReader(isr);
		     String line;
		     while ((line = br.readLine()) != null){
		        sb.append(line);
		     }
		}catch(Exception e){
			e.printStackTrace();
		}
		String json = sb.toString();
		json = json.split("\"data\":")[1].split(",\"ext\":null}")[0];
		List<SeatInfo> infos = new ArrayList<>();
		infos = JSON.parseArray(json, SeatInfo.class);
		
		for(SeatInfo info : infos) {
			if(info.getTs().size() == 0) {
				continue;
			}else {
				for(Student st : info.getTs()) {
					Seat seat = new Seat();
					seat.setAccno(st.getAccno());
					seat.setCreateTime(MyUtils.getNowDateTime());
					seat.setDevId(info.getDevId());
					seat.setDevName(info.getDevName());
					seat.setEnd(st.getEnd());
					seat.setOwner(st.getOwner());
					seat.setStart(st.getStart());
					seat.setState(st.getState());
					
					if(seatService.count(seat.getDevId(), seat.getAccno(), seat.getStart()) <= 0) {
						seatService.insert(seat);
					}					
				}
			}
		}
	}
}
